/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Object.Book;
import Object.BookProperties.Author;
import Object.BookProperties.Type;
import Object.CustomerOrder;
import Object.Page;
import Object.User;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author PhongLK60473
 */
public class ManagerController extends HttpServlet {

    private String page_index = "index.jsp";
    private String page_checkOrders = "checkorders.jsp";
    private String page_manageBooks = "managebooks.jsp";
    private String page_editBooks = "editbook.jsp";
    private String page_manageTypes = "managebooktypes.jsp";
    private String page_manageAuthors = "managebookauthors.jsp";
    private String page_manageAccounts = "manageaccounts.jsp";
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action = request.getParameter("action");
            HttpSession sess = request.getSession();
            User oldUser = (User) sess.getAttribute("User");
            System.out.println(action);
            if (action == null || action.equals("") || action.equals("Home") || action.equals("CheckOrders")) {
                if (oldUser==null||oldUser.isAdmin()==false) {
                    response.sendRedirect(page_index);
                } else {
                    Page.forward(page_checkOrders, request, response);
                }
            } else if (action.equals("ManageBooks")) {
                Page.forward(page_manageBooks, request, response);
            } else if (action.equals("Search")) {
                Page.forward(page_manageBooks, request, response);
            } else if (action.equals("EditBook")) {
                Page.forward(page_editBooks, request, response);
            } else if (action.equals("Complete Edit")) {
                if (Book.SaveBook(request)) {
                    Page.forward(page_manageBooks, request, response);
                } else {
                    Page.forward(page_editBooks, request, response);
                }
            } else if (action.equals("Add Book")) {
                Book.AddBook(request);
                Page.forward(page_manageBooks, request, response);
            } else if (action.equals("DeleteBook")) {
                Book.DeleteBook(request);
                Page.forward(page_manageBooks, request, response);
            } else if (action.equals("Edit Type")) {
                Type.SaveType(request);
                Page.forward(page_manageTypes, request, response);
            } else if (action.equals("Add Type")) {
                Type.AddType(request);
                Page.forward(page_manageTypes, request, response);
            } else if (action.equals("DeleteType")) {
                Type.DeleteType(request);
                Page.forward(page_manageTypes, request, response);
            } else if (action.equals("ManageTypes")) {
                Page.forward(page_manageTypes, request, response);
            } else if (action.equals("Edit Author")) {
                Author.SaveAuthor(request);
                Page.forward(page_manageAuthors, request, response);
            } else if (action.equals("Add Author")) {
                Author.AddAuthor(request);
                Page.forward(page_manageAuthors, request, response);
            } else if (action.equals("DeleteAuthor")) {
                Author.DeleteAuthor(request);
                Page.forward(page_manageAuthors, request, response);
            } else if (action.equals("ManageAuthors")) {
                Page.forward(page_manageAuthors, request, response);
            } else if (action.equals("ChangeStatus")) {
                CustomerOrder.changeStatus(request);
                Page.forward(page_checkOrders, request, response);
            } else if (action.equals("ManageAccounts")) {
                Page.forward(page_manageAccounts, request, response);
            } else if (action.equals("ActiveUser")||action.equals("DeactiveUser")) {
                User.activeUser(request);
                Page.forward(page_manageAccounts, request, response);
            } else {
                response.sendRedirect(page_index);
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
